package com.projet.instamiam;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class InstaMiamProvider extends ContentProvider {

	private static final UriMatcher sUriMatcher;

	private static final int DEFAULT = 0;
	private static final int PICTURE_PEOPLE = 1;
	private static final int PEOPLE_PICTURE = 2;

	static {
		sUriMatcher = new UriMatcher(DEFAULT);
		sUriMatcher.addURI(InstaMiamContract.AUTHORITY,
				InstaMiamContract.Annotation.TABLE_NAME, DEFAULT);
		sUriMatcher.addURI(InstaMiamContract.AUTHORITY,
				InstaMiamContract.Annotation.TABLE_NAME+"/picture/#", PICTURE_PEOPLE);
		sUriMatcher.addURI(InstaMiamContract.AUTHORITY,
				InstaMiamContract.Annotation.TABLE_NAME+"/people/#", PEOPLE_PICTURE);
		}

	private static class AnnotDBHelper extends SQLiteOpenHelper {

		static final int VERSION = 1;
		static final String DBNAME = "instamiam.db";

		public AnnotDBHelper(Context context) {
			super(context, DBNAME, null, VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + InstaMiamContract.Annotation.TABLE_NAME
					+ " ( " + InstaMiamContract.Annotation._ID
					+ " INTEGER PRIMARY KEY, " + InstaMiamContract.Annotation.PEOPLE_ID
					+ " INTEGER NOT NULL, " + InstaMiamContract.Annotation.PICTURE_ID + ")");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		}

	}

	private AnnotDBHelper dbHelper;
	
	@Override
	public boolean onCreate() {
		this.dbHelper = new AnnotDBHelper(getContext());
		return false;
	}

	@Override
	public String getType(Uri uri) {

		switch (sUriMatcher.match(uri)) {
		case PEOPLE_PICTURE:
		case PICTURE_PEOPLE:
			return InstaMiamContract.Annotation.CONTENT_TYPE;
		default:
			throw new IllegalArgumentException("Unknown Uri : "
					+ uri.toString());
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		
		String tableName = null;

		switch (sUriMatcher.match(uri)) {
		case PEOPLE_PICTURE:
		case PICTURE_PEOPLE:
			tableName = InstaMiamContract.Annotation.TABLE_NAME;
			break;
		default:
			throw new IllegalArgumentException("Unknown Uri : "
					+ uri.toString());
		}
		
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		return db.delete(tableName, selection, selectionArgs);
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		String tableName = null;
		Uri newUri = null;

		switch (sUriMatcher.match(uri)) {
		case PEOPLE_PICTURE:
		case PICTURE_PEOPLE:
			tableName = InstaMiamContract.Annotation.TABLE_NAME;
			break;
		default:
			throw new IllegalArgumentException("Unknown Uri : "
					+ uri.toString());
		}

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		long id = db.insert(tableName, null, values);

		if (id > -1 && newUri == null) {
			newUri = Uri.withAppendedPath(uri, String.valueOf(id));
		}
		return newUri;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		String tableName = null;
		Cursor c = null;
		String[] columns = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();

		switch (sUriMatcher.match(uri)) {
		case PEOPLE_PICTURE:
		case PICTURE_PEOPLE:
			tableName = InstaMiamContract.Annotation.TABLE_NAME;
			columns = new String[] { InstaMiamContract.Annotation.PEOPLE_ID,
					InstaMiamContract.Annotation.PICTURE_ID };
			break;
		default:
			throw new IllegalArgumentException("Unknown Uri : "
					+ uri.toString());
		}

		c = db.query(tableName, columns, selection, selectionArgs, sortOrder,
				null, null);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		return 0;
	}

}